package viewmodel;

import items.BetSideItem;

import java.util.ArrayList;

import model.Bet;
import model.BetDecisionType;
import model.BetSide;
import model.BetVisibility;
import android.app.betmein.R;
import android.app.betmein.view.CreateBetView;
import android.app.betmein.view.ViewCreatorHelper;
import android.content.Context;
import android.framework.binding.BindableValue;
import android.framework.binding.Binding;
import android.framework.binding.ItemList;
import android.framework.binding.converters.ConverterIntegerString;
import android.framework.binding.converters.IConverter;
import android.framework.paging.BaseViewModel;
import android.framework.reflaction.annotation.OnClickListenerInjectionAnnotation;
import android.widget.ArrayAdapter;

public class CreateBetViewModel extends BaseViewModel<CreateBetView> {

	private Bet bet = new Bet();
	private ItemList<BetSideItem> betSideItem;
	
	public CreateBetViewModel(Context context) {
		super(context);
		this.betSideItem = new ItemList<BetSideItem>(context, R.layout.bet_side, ViewCreatorHelper.betSideListViewCreator());
	}

	@Override
	public void onInit() {
		getView().betSides.setAdapter(this.betSideItem);
		
		//Participates
		//Binding.attached(getView().entities,bet.getEntityList());    
		
		// Title
		Binding.attached(getView().title ,bet.getTitle());       	  
		//Description
		Binding.attached(getView().description,bet.getDescription()); 
		//Amount
		Binding.attached(bet.getBetAmount(),getView().moneyAmount,new ConverterIntegerString()); 
		//Visibility
		Binding.attached(bet.getBetVisibility(), getView().visibility,
				new IConverter<BetVisibility, String>() {public String convertTo(BetVisibility value)
				{return value.toString();}public BetVisibility convertFrom(String value) {
					return Enum.valueOf(BetVisibility.class, value);}});
		ArrayList<String> list = new ArrayList<String>();
		for (BetVisibility v : BetVisibility.values()) {
			list.add(v.toString());
		}
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, R.layout.test_item, list);
		getView().visibility.setAdapter(adapter);
		
		
		//Add Side 
		//Binding.attached(bet.getBetSides(),getView().betSides);
		
		//Decision Time  
		Binding.attached(bet.getDecisionTime(), getView().decisionTime);
		//Last Date For Bet
		Binding.attached(bet.getLastDateForBet(), getView().lastTimeForBet);
		
		//Decision Type
		Binding.attached(bet.getDecisionType(), getView().decisionType,
				new IConverter<BetDecisionType, String>() {public String convertTo(BetDecisionType value)
				{return value.toString();}public BetDecisionType convertFrom(String value) {
					return Enum.valueOf(BetDecisionType.class, value);}});
		ArrayList<String> decisionList = new ArrayList<String>();
		for (BetDecisionType v : BetDecisionType.values()) {
			decisionList.add(v.toString());
		}
		ArrayAdapter<String> decisionAdapter = new ArrayAdapter<String>(context, R.layout.test_item, decisionList);
		getView().decisionType.setAdapter(decisionAdapter);
		
		
	}
	
	
	@OnClickListenerInjectionAnnotation(R.id.cb_addSideButton)
	public void onClick(){
		BetSide betSide = new BetSide();
		betSide.getSideDescription().setText(getView().betSides.toString());
		betSideItem.add(new BetSideItem(new BindableValue<BetSide>(betSide)));
	}
	
	
	
}
